package com.zhika.zishi.adapter;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhika.zishi.R;
import com.zhika.zishi.beans.AnswererBean;
import com.zhika.zishi.util.LogUtil;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by shi-02 on 2015/4/21.
 */
public class AnswererAdapter extends BaseAdapter {

    private final String TAG = "AnswererAdapter";

    private ArrayList<AnswererBean> answererList;

    private Context context;

    private Handler handler;

    private HashMap<AnswererBean, Boolean> booleanHashMap = new HashMap<>();

    private HashMap<AnswererBean, ImageView> imageViewHashMap = new HashMap<>();

    public AnswererAdapter(Context context, ArrayList<AnswererBean> answerList, Handler handler) {

        this.answererList = answerList;

        this.context = context;

        this.handler = handler;

    }
    public AnswererAdapter(Context context, ArrayList<AnswererBean> answerList) {

        this.answererList = answerList;

        this.context = context;
    }



    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public int getCount() {
        return answererList.size();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final View view;
        final ViewHolder viewHolder;

        if (convertView == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_of_my_grid_view, null);
            viewHolder = new ViewHolder();
            viewHolder.headerImageView = (ImageView) view.findViewById(R.id.item_of_my_grid_view_header_image_view);
            viewHolder.stateImageView = (ImageView) view.findViewById(R.id.item_of_my_gird_view_state_image_view);
            viewHolder.nicknameText = (TextView) view.findViewById(R.id.item_of_my_grid_view_nickname_text);
            viewHolder.introductionText = (TextView) view.findViewById(R.id.item_of_my_gird_view_introduction_text);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
            resetViewHolder(viewHolder);
        }

        final AnswererBean bean = answererList.get(position);

        //如果这是在注册的时候
        if (bean.getType()!=null && bean.getType().equals("register")){
            viewHolder.introductionText.setVisibility(View.VISIBLE);
            viewHolder.introductionText.setText(bean.getIntroductionStr());
        }else {

            viewHolder.headerImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (booleanHashMap.get(bean) == null || !booleanHashMap.get(bean)) {
                        viewHolder.stateImageView.setBackgroundColor(context.getResources().getColor(R.color.color_yellow));
                        Message message = Message.obtain();
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("answerer", bean);
                        message.what = 1;
                        message.setData(bundle);
                        handler.sendMessage(message);

                        booleanHashMap.put(bean, true);
                    } else {

                    }

                }
            });
        }

        viewHolder.headerImageView.setImageResource(R.drawable.ic_launcher);

        viewHolder.nicknameText.setText(bean.getNicknameStr());

        imageViewHashMap.put(bean, viewHolder.stateImageView);



        return view;
    }

    private void resetViewHolder(ViewHolder holder) {
        holder.headerImageView.setImageBitmap(null);

//        holder.stateImageView.setImageBitmap(null);

        holder.nicknameText.setText(null);

        holder.introductionText.setText(null);
    }

    private class ViewHolder {

        private ImageView headerImageView;

        private ImageView stateImageView;

        private TextView nicknameText;

        private TextView introductionText;
    }

    /**
     * 当在已选中的用户中删除一个用户时，更新推荐用户的状态
     *
     * @param bean
     */
    public void changeStateImageView(AnswererBean bean) {

        ImageView imageView = imageViewHashMap.get(bean);
        int id = imageView.getId();
        LogUtil.e(TAG,"id="+id);
        imageView.setBackgroundColor(context.getResources().getColor(R.color.identify_false_color));
        booleanHashMap.put(bean, false);
    }
}
